package protocol.transactions;

import protocol.XoneProtocol;
import protocol.XoneServiceCode;
import protocol.meta.InfoPraise;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.netease.http.THttpRequest;

public class TransactionPraiseInfo extends XoneBaseTransaction {
    private String mInfoId;
    private boolean mPraiseFlag;

    public TransactionPraiseInfo(String infoId, boolean praiseFlag) {
        super(TRANSACTION_TYPE_PRAISEINFO);
        
        mInfoId = infoId;
        mPraiseFlag = praiseFlag;
    }

    @Override
    protected void onXoneTransactionSuccess(int code, Object obj) {
        InfoPraise infoPraise = null;
        if (obj instanceof JsonElement) {
            try {
                JsonElement jsonObj = ((JsonElement)obj).getAsJsonObject().get("praise");
                Gson gson = new Gson();
                infoPraise = gson.fromJson(jsonObj, InfoPraise.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, infoPraise);
    }

    @Override
    public void onTransact() {
        THttpRequest request = XoneProtocol.getInstance().createPraiseInfoRequest(mInfoId, mPraiseFlag);
        sendRequest(request);
    }
}
